package com.lleice.web.message.kafka.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Primary;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import com.lleice.common.entity.common.SaveRequestLog;
import com.lleice.common.strategy.MessageStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * kafka生产者
 * @author lizihao
 *
 */
@Service
@Primary
@ConditionalOnProperty(prefix = "spring.kafka", name = "enable", havingValue = "true")
public class KafkaProducer implements MessageStrategy{

	@Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    
    private static Gson gson = new GsonBuilder().create();

    /**
     * 发送消息
     * @param SaveRequestLog 消息封装
     */
	@Override
	public void saveRequestLog(SaveRequestLog saveRequestLog) {
		kafkaTemplate.send(saveRequestLog.getTopic(), gson.toJson(saveRequestLog.getObj()));
		
	}
}
